﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RandomNumberPractice : MonoBehaviour
{
    List<Vector3> temp;


    [ContextMenu("Example1_Random01_Fall")]
    void Example1_Random01_Fall()
    {
        temp = new List<Vector3>();
        for (int i = 0; i < 50; i++)
        {
            var r = Random01_Fall();
            temp.Add(new Vector3(r, 0, 0));
        }
    }

    [ContextMenu("Example1_Random02_Rise")]
    void Example2_Random02_Rise()
    {
        temp = new List<Vector3>();
        for (int i = 0; i < 50; i++)
        {
            var r = Random01_Rise();
            temp.Add(new Vector3(r, 0, 0));
        }
    }

    [ContextMenu("Example3_Random03_Arc")]
    void Example3_Random03_Arc()
    {
        temp = new List<Vector3>();
        for (int i = 0; i < 50; i++)
        {
            var r = Random01_Arc(0, 1.5f);
            temp.Add(new Vector3(r, 0, 0));
        }
    }

    [ContextMenu("Example4_EliminateRepeatRandom")]
    void Example3_EliminateRepeatRandom()
    {
        temp = new List<Vector3>();
        for (int i = 0, last = -1; i < 50; i++)
        {
            last = EliminateRepeatRandom(last, 0, 10);

            Debug.Log("last: " + last);
        }
    }

    void OnDrawGizmos()
    {
        if (temp == null) return;

        for (int i = 0; i < temp.Count; i++)
        {
            var x = temp[i];

            Gizmos.DrawWireSphere(x, 0.001f);
        }
    }

    public float Random01_Fall()
    {
        var r1 = Random.value;

        return Random.Range(1 - r1, 1 + Mathf.Epsilon);
    }

    public float Random01_Rise()
    {
        var r1 = Random.value;

        return Random.Range(0, 1 - r1);
    }

    public float Random01_Arc(float averageOffset = 0, float sigma = 2f)
    {
        var r1 = Random.value;

        var t1 = Mathf.Lerp(0, 1, r1);
        var t2 = Mathf.Lerp(1, 0, r1);
        var tFinal = Mathf.Lerp(t1, t2, r1) * sigma;

        var r2 = Mathf.Lerp(r1, 0.5f, tFinal) + averageOffset;

        return r2;
    }

    public int EliminateRepeatRandom(int last, int min, int max)
    {
        var current = Random.Range(min, max);

        if (last == current)
            return (current + (int)Mathf.Sign(Random.value) * Random.Range(min + 1, max - 1)) % max;
        else
            return current;
    }
}
